<?php

namespace Admin\Controller;

class EnterpriseController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Enterprise");
    }

    /**
     *  管理员管理，有add添加，edit修改，delete删除
     */
    function index() {
        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $enterprise = D("enterprise")->where(array('enterprise_code'=>$user["enterprise_code"]))->find();
        if($enterprise["level"] != 0){
            $requestMap["ep.enterprise_code"] = $user["enterprise_code"];
        }

        $count = $this->currentModel->count();

        $page = $this->page($count, 20);
        $list = $this->currentModel->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);

        $allRoleList = D("AdminRole")->select();
        $this->assign("enterprise", $enterprise);
        $this->assign("allRoleList", $allRoleList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();
    }


    function add() {

        $roles = D("AdminRole")->where(array("level" => "1"))->order("id asc")->select();

        $processArr = array("取阴模", "检查取阴模","修阳模","检查修阳模","热塑成型","检查热塑成型","打磨抛光","检查打磨抛光","组装对线","半成品适配","穿戴及使用训练","入库");

        $list = array();
        $i = 0;
        foreach ($processArr as $process){
            $list[] = array("status_id" => $i, "name" => $process, "status" => "0");
            $i++;
        }

        $this->assign("list", $list);
        $this->assign("roles", $roles);
        $this->display();
    }

    function add_post() {
        if (IS_POST) {
            if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {

                $enterprise = $this->currentModel->where(array('enterprise_name' => I('post.enterprise_name')))->find();
                if(!isNullOrEmpty($enterprise)) {
                    $this->error("该企业名已存在");
                }

                $role_ids = I('post.role_id');

                $enterprise["image_url"] = I('post.signimgurl');
                $enterprise['enterprise_name'] = I('post.enterprise_name');
                $enterprise['enterprise_code'] = I('post.enterprise_code');
                $enterprise['workorder_no_head'] = I('post.workorder_no_head');

                $enterprise['role_list'] = "";
                $enterprise['create_time'] = getCurrentDate();
                $enterprise['update_time'] = getCurrentDate();
                $users_obj= M("admin");
                $userId=$_SESSION['ADMIN_ID'];
                $user=$users_obj->where("user_id=$userId")->find();
                $enterprise['update_id'] = $user["employee_id"];

                $enterprise['role_list'] = "";
                foreach ($role_ids as $role_id) {
                    $enterprise['role_list'].=$role_id;
                    if($role_id != end($role_ids)) {
                        $enterprise['role_list'].=",";
                    }
                }
                $enterprise["orthosis_process"] = I('post.orthosisStatusId');
                $this->currentModel->add($enterprise);

                $this->success("保存成功！", U("Enterprise/index"));
            } else {
                $this->error("请为企业指定角色！");
            }
        }
    }

    function edit() {
//        $userId = intval(I("get.id"));
        $id = $_REQUEST["id"];

//        $role_ids = $this->roleUserModel->where(array("user_id" => $userId))->getField("role_id", true);
        $enterprise = $this->currentModel->queryById($id);

        $role_ids = explode(',', $enterprise["role_list"]);

        $updater = D("Admin")->where(array("employee_id" => $enterprise["update_id"]))->find();

        $roles = D("AdminRole")->where(array("level" => $enterprise["level"]))->order("id asc")->select();

        $processArr = array("取阴模", "检查取阴模","修阳模","检查修阳模","热塑成型","检查热塑成型","打磨抛光","检查打磨抛光","组装对线","半成品适配","穿戴及使用训练","入库");
        $orthosisStatusArr=explode(",",$enterprise["orthosis_process"]);

        $list = array();
        $i = 0;
        foreach ($processArr as $process){
            $list[] = array("status_id" => $i, "name" => $process, "status" => $orthosisStatusArr[$i]);
            $i++;
        }
        $this->assign("id", $id);
        $this->assign("list", $list);
        $this->assign("enterprise", $enterprise);
        $this->assign("roles", $roles);
        $this->assign("role_ids", $role_ids);
        $this->assign('updateName', $updater["admin_name"]);
        $this->display();
    }

    /**
     *  编辑管理员
     */
    function edit_post() {
        if (IS_POST) {
            if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
                $role_ids = I('post.role_id');
                $enterprise = $this->currentModel->queryById($_POST['id']);
                $enterprise["image_url"] = I('post.signimgurl');
                $enterprise['enterprise_name'] = I('post.enterprise_name');
                $enterprise['enterprise_code'] = I('post.enterprise_code');
                $enterprise['workorder_no_head'] = I('post.workorder_no_head');

                $enterprise['role_list'] = "";
                $enterprise['update_time'] = getCurrentDate();
                $users_obj= M("admin");
                $userId=$_SESSION['ADMIN_ID'];
                $user=$users_obj->where("user_id=$userId")->find();
                $enterprise['update_id'] = $user["employee_id"];


                $enterprise['role_list'] = "";
                foreach ($role_ids as $role_id) {
                    $enterprise['role_list'].=$role_id;
                    if($role_id != end($role_ids)) {
                        $enterprise['role_list'].=",";
                    }
                }
                $enterprise["orthosis_process"] = I('post.orthosisStatusId');
                $this->currentModel->updateInfo($enterprise);

                $this->success("保存成功！", U("Enterprise/index"));
            } else {
                $this->error("请为企业指定角色！");
            }
        }
    }

    /**
    *  删除
    */
   function delete() {
       $id = $_REQUEST['id'];
       $this->currentModel->deleteById($id);

       $this->success('删除成功!', U("Enterprise/index"));
   }

}